package com.ktgj.customer.service.impl;

import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.domain.Rightdetail;
import com.ktgj.customer.domain.RightdetailServices;
import com.ktgj.customer.domain.vo.RightDetailServicesVo;
import com.ktgj.customer.mapper.RightdetailMapper;
import com.ktgj.customer.mapper.RightdetailServicesMapper;
import com.ktgj.customer.service.IRightdetailService;
import com.ktgj.service.api.domain.AirportServiceApi;
import com.ktgj.service.api.domain.VipboardingApi;
import com.ktgj.service.api.domain.VipchannelApi;
import com.ktgj.service.api.domain.ViphallApi;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 权益Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-30
 */
@Service
public class RightdetailServiceImpl implements IRightdetailService 
{
    @Autowired
    private RightdetailMapper rightdetailMapper;

    @Autowired
    private RightdetailServicesMapper rightdetailServicesMapper; //权益服务范围（中间表）

    /**
     * 查询权益
     * 
     * @param rightdetailId 权益ID
     * @return 权益
     */
    @Override
    public Rightdetail selectRightdetailById(Long rightdetailId)
    {
        return rightdetailMapper.selectRightdetailById(rightdetailId);
    }

    /**
     * 查询权益列表
     * 
     * @param rightdetail 权益
     * @return 权益
     */
    @Override
    public List<Rightdetail> selectRightdetailList(Rightdetail rightdetail)
    {
        return rightdetailMapper.selectRightdetailList(rightdetail);
    }

    /**
     * 新增权益
     * 
     * @param rightdetail 权益
     * @return 结果
     */
    @Override
    public int insertRightdetail(Rightdetail rightdetail)
    {
        rightdetail.setCreateTime(DateUtils.getNowDate());
        return rightdetailMapper.insertRightdetail(rightdetail);
    }

    /**
     * 修改权益
     * 
     * @param rightdetail 权益
     * @return 结果
     */
    @Override
    public int updateRightdetail(Rightdetail rightdetail)
    {
        rightdetail.setUpdateTime(DateUtils.getNowDate());
        return rightdetailMapper.updateRightdetail(rightdetail);
    }

    /**
     * 批量删除权益
     * 
     * @param rightdetailIds 需要删除的权益ID
     * @return 结果
     */
    @Override
    public int deleteRightdetailByIds(Long[] rightdetailIds)
    {
        return rightdetailMapper.deleteRightdetailByIds(rightdetailIds);
    }

    /**
     * 删除权益信息
     * 
     * @param rightdetailId 权益ID
     * @return 结果
     */
    @Override
    public int deleteRightdetailById(Long rightdetailId)
    {
        return rightdetailMapper.deleteRightdetailById(rightdetailId);
    }

    /**
     * 根据卡id查询卡有那些权益
     *
     * @param cardId
     * @return
     */
    @Override
    public List<Rightdetail> selectRightdetailByCardId(Long cardId) {
        return rightdetailMapper.selectRightdetailByCardId(cardId);
    }

    /**
     * 添加权益服务范围
     * @param rightDetailServicesVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveRightDetailService(RightDetailServicesVo rightDetailServicesVo) {

        int count = 0;

        //获取权益数据
        Rightdetail rightdetail = new Rightdetail();
        BeanUtils.copyProperties(rightDetailServicesVo,rightdetail);
        //时间
        rightdetail.setCreateTime(DateUtils.getNowDate());
        //保存权益信息
        count = rightdetailMapper.insertRightdetail(rightdetail);


        //获取权益id
        Long rightdetailId = rightdetail.getRightdetailId();

        //贵宾厅
        if(rightDetailServicesVo.getServiceType() == 1){
            //获取页面提交的范围范围数据
            List<ViphallApi> viphallApiList = rightDetailServicesVo.getViphallList();
            if(viphallApiList !=null && viphallApiList.size()>0){
                viphallApiList.forEach(vipHall ->{
                    RightdetailServices rightdetailServices = new RightdetailServices();
                    rightdetailServices.setRightdetailId(rightdetailId); //权益id
                    rightdetailServices.setServicesId(vipHall.getViphallId()); //服务id
                    rightdetailServices.setServiceType(rightDetailServicesVo.getServiceType());
                    //保存权益服务范围信息
                    rightdetailServicesMapper.insertRightdetailServices(rightdetailServices);
                });
            }
        }
        //机场服务
        else if(rightDetailServicesVo.getServiceType() == 2){
            List<AirportServiceApi> airportServiceApiList = rightDetailServicesVo.getAirportServiceList();
            if(airportServiceApiList !=null && airportServiceApiList.size()>0){
                airportServiceApiList.forEach(airportServiceApi ->{
                    RightdetailServices rightdetailServices = new RightdetailServices();
                    rightdetailServices.setRightdetailId(rightdetailId); //权益id
                    rightdetailServices.setServicesId(airportServiceApi.getAirportServiceId()); //服务id
                    rightdetailServices.setServiceType(rightDetailServicesVo.getServiceType());
                    //保存权益服务范围信息
                    rightdetailServicesMapper.insertRightdetailServices(rightdetailServices);
                });
            }
        }
        //值机
        else if(rightDetailServicesVo.getServiceType() == 3){
            List<VipboardingApi> vipboardingApiList = rightDetailServicesVo.getVipboardingList();
            if(vipboardingApiList !=null && vipboardingApiList.size()>0){
                vipboardingApiList.forEach(vipboardingApi ->{
                    RightdetailServices rightdetailServices = new RightdetailServices();
                    rightdetailServices.setRightdetailId(rightdetailId); //权益id
                    rightdetailServices.setServicesId(vipboardingApi.getBoardingId()); //服务id
                    rightdetailServices.setServiceType(rightDetailServicesVo.getServiceType());
                    //保存权益服务范围信息
                    rightdetailServicesMapper.insertRightdetailServices(rightdetailServices);
                });
            }
        }
        //vip通道
        else if(rightDetailServicesVo.getServiceType() == 4){
            List<VipchannelApi> vipchannelApiList = rightDetailServicesVo.getVipchannelList();
            if(vipchannelApiList !=null && vipchannelApiList.size()>0){
                vipchannelApiList.forEach(vipchannelApi ->{
                    RightdetailServices rightdetailServices = new RightdetailServices();
                    rightdetailServices.setRightdetailId(rightdetailId); //权益id
                    rightdetailServices.setServicesId(vipchannelApi.getVipchannelId()); //服务id
                    rightdetailServices.setServiceType(rightDetailServicesVo.getServiceType());
                    //保存权益服务范围信息
                    rightdetailServicesMapper.insertRightdetailServices(rightdetailServices);
                });
            }
        }
        return count;
    }

    /**
     * 删除权益服务范围
     * @param rightDetailId 权益id
     * @param serviceType 服务类型
     * @param serviceId 服务id
     * @return
     */
    @Override
    public int removeRightDetailServiceChange(Long rightDetailId, Long serviceType, Long[] serviceId) {
        //先获取数据
        List<RightdetailServices> servicesList = rightdetailServicesMapper.selectServicesChangeByRightDetailIdAndserviceTypeAndserviceId(rightDetailId,serviceType,serviceId);

        //执行删除操作
        int count = 0;
        if(servicesList!=null && servicesList.size()>0){
            count = (int) servicesList.stream().mapToInt(RightDetailServices -> rightdetailServicesMapper.deleteRightdetailServicesById(RightDetailServices.getId())).count();

        }
        return count;
    }
}
